#
# Cookbook Name:: om
# Recipe:: build_agent
#
# Copyright 2013, IBM
#
# All rights reserved - Do Not Redistribute
#


wasuser = node[:was][:user]
wasgroup = node[:was][:group]
wasuserpw = node[:was][:userpw]
cuser = node[:chef][:user]
cgroup = node[:chef][:group]
deployment = node[:deployment]

class Chef::Recipe
include Orchestrate
end

zkep = "#{node[:zookeeper][:ip]}:#{node[:zookeeper][:port]}"
chef_gem 'zk'


startinstr "Build OM EAR"

bash "Build the ear" do
	user wasuser
	group wasgroup
	cwd "#{node[:om][:installDir]}"
	creates "#{node[:om]['installDir']}/.installcomplete/earbuild.complete"
	timeout 36000
	code <<-EOH
	  set -e
		#{node[:om][:installDir]}/bin/buildear.sh #{node[:common][:buildEarParams]} > /tmp/earbuild.log 2>&1
    chmod -R 755 #{node[:om][:installParentDir]}
		mkdir -p #{node[:om][:installDir]}/.installcomplete
		touch #{node[:om][:installDir]}/.installcomplete/earbuild.complete
	EOH
end

cuser = node[:chef][:user]
cgroup = node[:chef][:group]
bash "Upload the Runtime EAR to the file server" do
    user cuser
    group cgroup
    cwd "/tmp/"
    code <<-EOH
        ssh #{node[:common][:scpuser]}@#{node[:common][:scpip]} "mkdir -p #{node[:common][:scp_repo_dir]}/generated/#{deployment}"
        scp #{node[:om][:installDir]}/external_deployments/#{node[:common][:earName]} #{node[:common][:scpuser]}@#{node[:common][:scpip]}:#{node[:common][:scp_repo_dir]}/generated/#{deployment}/#{node[:common][:earName]}
    EOH
end


ruby_block "Publishing ear build completion status to ZooKeeper." do
    block do
        i_url = "/deployments/" + deployment + "/config/om/runtime/application/ear/status"
        dep = Dependency.new(i_url, node[:client][:status], zkep)
        dep.publish!
    end
end

stopinstr "Build OM EAR"



